﻿
Imports std

Partial Public Class operationJobConsumer
    Private Shared ReadOnly removeIntervalMs As Int64 = secondsToMilliseconds(5)

    Private Function should_remove(ByVal job As job) As Boolean
        Return job Is Nothing OrElse
               (job.exited() AndAlso (Now.milliseconds() - job.exitMs()) > reserveJobInfoMilliseconds())
    End Function

    Private Sub removeOldJobInfo()
        assert_begin(New eventComb(Function() As Boolean
                                       Return waitfor(removeIntervalMs) AndAlso goto_next()
                                   End Function,
                                   Function() As Boolean
                                       lock_jobs()
                                       Dim it As map(Of String, job).iterator = Nothing
                                       it = jobs.begin()
                                       While it <> jobs.end()
                                           If should_remove((+it).second) Then
                                               assert(jobs.erase(it))
                                               Exit While
                                           End If
                                           it += 1
                                       End While
                                       unlock_jobs()
                                       Return goto_prev()
                                   End Function))
    End Sub
End Class
